package d.n.d.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import d.n.d.constant.Constant;
import d.n.d.domain.Account;
import d.n.d.domain.Customer;
import d.n.d.service.AccountService;
import d.n.d.service.CustomerService;
import d.n.d.service.TransactionService;
import d.n.d.service.UserService;

/**
 * 
 * @author D.N.D
 * 
 */
@Controller
public class BaseController {

	@Autowired
	private TransactionService transactionService;

	@Autowired
	private AccountService accountService;

	@Autowired
	private UserService userService;

	@Autowired
	private CustomerService customerService;

	Date from = null, to = null;
	String stringDateFrom;
	String stringDateTo;
	String stringIdCard;

	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/searchAccount")
	public String doSeachAccount() {

		return "searchAccount";
	}

	/**
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/searchIDCard", method = RequestMethod.POST)
	public String searchIDCard(HttpServletRequest request, Model model) {

		String url;
		HttpSession session = request.getSession();
		String idCard = request.getParameter("txtSearchAccount");
		Customer tempCustomer = customerService.searchCustomerByIDCard(idCard);

		if ("".equals(idCard)) {
			model.addAttribute("error", "Please Input Id card!");
			url = "searchAccount";
		} else if (tempCustomer == null) {
			model.addAttribute("error", "ID Card is not exist!");
			url = "searchAccount";
		} else {
			System.out.println("d2");
			String idCardNumber = tempCustomer.getIdCardNumber();
			List<Account> accountList = accountService
					.getAccountListByIdCardNumber(idCardNumber);
			session.setAttribute("accountIdCard", accountList);
			model.addAttribute("accounts", accountList);
			url = "searchAccount";
		}
		return url;
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/viewDetailAccount")
	public @ResponseBody Account viewDetailAccount(HttpServletRequest request) {
		String accountNumber = request.getParameter("accountNumber");
		Account account = accountService
				.findAccountInfoByAccountInfoNumber(accountNumber);
		account.getIdCardNumber().setAccounts(null);
		return account;
	}

	/**
	 * 
	 * @return
	 */
	@RequestMapping(value = "/viewTransaction")
	public String doViewTransaction() {

		return "viewTransaction";
	}

	/**
	 * 
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/searchTransaction", method = RequestMethod.POST)
	public String searchTransaction(Model model, HttpServletRequest request) {

		stringIdCard = request.getParameter("idcard");
		stringDateFrom = request.getParameter("fday");
		stringDateTo = request.getParameter("tday");
		if ("".equals(stringDateFrom) && "".equals(stringDateTo)) {
			from = null;
			to = null;
			return transactionService.searchTransactionByIdCard(model,
					stringIdCard);
		} else {
			if (!"".equals(stringDateFrom) && !"".equals(stringDateTo)) {
				if (!transactionService.isValidDate(stringDateFrom,
						stringDateTo)) {
					model.addAttribute("error", "From Date < To Date!");
					return "viewTransaction";
				}
			}
			if (!"".equals(stringDateFrom)) {
				try {
					from = new SimpleDateFormat(Constant.SIMPLE_DATE_FORMAT)
							.parse(stringDateFrom + " 00:00:00");
				} catch (ParseException e) {
					e.printStackTrace();
				}
			} else {
				try {
					from = new SimpleDateFormat(Constant.SIMPLE_DATE_FORMAT)
							.parse("2010-01-01 00:00:00");
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (!"".equals(stringDateTo)) {
				try {
					to = new SimpleDateFormat(Constant.SIMPLE_DATE_FORMAT)
							.parse(stringDateTo + " 23:59:59");
				} catch (ParseException e) {
					e.printStackTrace();
				}
			} else {
				to = new Date();
			}
		}
		return transactionService
				.searchTransactionByIdCard(model, stringIdCard);
	}

	/**
	 * <br> Author: DucDS </br>
	 * <br> Method view transaction when click IDCardnumber. </br>
	 * @param request
	 * @param model
	 * @param accountId
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/viewTransactionByAccountId", method = RequestMethod.GET)
	public String viewTransaction(HttpServletRequest request, Model model,
			@RequestParam("accountId") String accountId,
			HttpServletResponse response) {

		return transactionService
				.viewTransactionByAccountNumber(model, stringIdCard, accountId, from, to, stringDateFrom, stringDateTo);
	}
}